﻿package makeeasier.engines {
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.utils.*;
	
	public class Perspective extends Sprite {
		
		private var Worker:Sprite;
		public var Container:Sprite;
		public var ForgroundObject:Sprite;
		public var BackgroundObject:Sprite;
		private var ForgroundComplete:Boolean;
		private var BackgroundComplete:Boolean;
		private var RangeData:Dictionary;
		private var FixData:Dictionary;
		private var FixDataY:Dictionary;
		private var CurrentData:Dictionary;
		private var p:Number;
		private var _stage:Stage;
		private var timer:Timer;
		private var AnimSpeed:Number;
		
		public function Perspective(){
			p = 3;
			RangeData = new Dictionary();
			FixData = new Dictionary();
			FixDataY = new Dictionary();
			CurrentData = new Dictionary();
			Container = new Sprite();
			Container.addEventListener(Event.ADDED, OnItemAdd);
			Container.addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
			Container.opaqueBackground = true;
			addChild(Container);
			ForgroundComplete = false;
			BackgroundComplete = false;
		}
		
		public function Prepare():void{
			//stage.align = StageAlign.LEFT;
			stage.addEventListener(Event.RESIZE, OnResize);
			Worker = new Sprite();
			Worker.y = stage.stageHeight/2
			Worker.graphics.beginFill(0xFFFFFF);
			Worker.graphics.drawRect(0,0,50,50);
			Worker.visible = false;
			addChild(Worker);
			//addEventListener("enterFrame",SetWorker);
			//addEventListener("enterFrame",SetPerspective);
			timer = new Timer(0);
			timer.addEventListener(TimerEvent.TIMER, SetWorker);
			timer.addEventListener(TimerEvent.TIMER, SetPerspective);
			timer.start();
			OnResize(null);
		}
		
		private function SetWorker(e:Event):void{
			Worker.x += (stage.mouseX-Worker.x) * 0.03;
			//trace(Worker.x);
		}
		
		public function GetForground():*{
			return ForgroundObject;
		}
		
		public function SetForground(obj:*):void{
			ForgroundObject = obj;
			ForgroundComplete = true;
			OnItemAdd(null);
		}
		
		public function GetBackground():*{
			return BackgroundObject;
		}
		
		public function SetBackground(obj:*):void{
			BackgroundObject = obj;
			BackgroundComplete = true;
			OnItemAdd(null);
		}
		
		public function AddItem(obj:DisplayObject, range:Number = 0.2, fixX:Number = 0, fixY:Number = 0):void{
			Container.addChild(obj);
			//trace("------------------------\nITEM ADDED: "+obj+"\nwidth: "+obj.width+"\n----------------------------\n");
			RangeData[obj] = range;
			FixData[obj] = fixX;
			CurrentData[obj] = fixX;
			FixDataY[obj] = fixY;
		}
		
		public function RemoveItem(obj:DisplayObject):void{
			Container.removeChild(obj);
		}
		
		public function get PerspectiveValue():Number{
			return p;
		}
		
		public function set PerspectiveValue(n:Number):void{
			p = n;
		}
		
		private final function OnItemAdd(e:Event):void{
			/*
			if( ForgroundComplete ){
				Container.setChildIndex(ForgroundObject,Container.numChildren-1);
			}
			if( BackgroundComplete ){
				Container.setChildIndex(BackgroundObject,0);
			}
			*/
		}
		
		private final function OnAddedToStage(e:Event):void{
			//stage.align = StageAlign.BOTTOM;
		}

		public final function OnResize(e:Event):void{
			x = 0;
			y = 0;
			var dis = stage.stageHeight;
			for( var i=Container.numChildren-1; i > 0; i-- ){
				if( Container.getChildAt(i) != ForgroundObject && Container.getChildAt(i) != BackgroundObject && RangeData[Container.getChildAt(i)] != 0 ){
					CurrentData[Container.getChildAt(i)] = GetCenter(Container.getChildAt(i)) + FixData[Container.getChildAt(i)];
				}
			}
			for( var j=0; j < Container.numChildren; j++ ){
				Container.getChildAt(j).y = stage.stageHeight - Container.getChildAt(j).height + FixDataY[Container.getChildAt(j)];
			}
			if(BackgroundComplete){
				BackgroundObject.x = 0;
				BackgroundObject.y = 0;
				BackgroundObject.width = stage.stageWidth;
				BackgroundObject.height = stage.stageHeight;
			}
			if(ForgroundComplete){
				ForgroundObject.x = ForgroundObject.width-stage.stageWidth;
			}
			SetPerspective(null);
		}
		
		public final function SetPerspective(e:Event):void{
			var screenCenterPointX = (-(stage.stageWidth/2)+Worker.x);
			//var screenCenterPointY = (-(stage.stageHeight/2)+Worker.y);
			//trace(screenCenterPointX);
			for( var i=Container.numChildren-1; i > 0; i-- ){
				if( Container.getChildAt(i) != ForgroundObject && Container.getChildAt(i) != BackgroundObject && RangeData[Container.getChildAt(i)]!=0){
					Container.getChildAt(i).x = (CurrentData[Container.getChildAt(i)] - (screenCenterPointX * RangeData[Container.getChildAt(i)]));
					//trace(Container.getChildAt(i)+": "+Container.getChildAt(i).x);
				}
			}
			if(ForgroundComplete){
				Container.getChildAt(Container.getChildIndex(ForgroundObject)).x = -(Container.getChildAt(Container.getChildIndex(ForgroundObject)).width-stage.stageWidth) * ((Worker.x)/stage.stageWidth);
			}
		}
		
		private final function GetCenter(item:*):Number{
			return (stage.stageWidth/2)-(item.width/2);
		}
	}
	
}